/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.event.KeyEvent;
import mx.edu.itschapala.sistemas.septimo.control.*;
import mx.edu.itschapala.sistemas.septimo.modelo.*;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.*;

/**
 *
 * @author Miguel Romero
 */
public class ClienteCrearEditar extends javax.swing.JFrame {

    /**
     * Creates new form ClienteCrearEditar
     */
    
    private ClienteLnLocal clienteControl;
    private TipoVentana tipoVentana;
    private Cliente cliente;
    
    public ClienteCrearEditar(Cliente cliente, ClienteLnLocal clienteControl, TipoVentana tipoVentana) {
        this.clienteControl = clienteControl;
        this.tipoVentana = tipoVentana;
        this.cliente=cliente;
        initComponents();
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                jLabel6.setText("REGISTRAR UN CLIENTE");
                break;
            case VENTANA_MODIFICAR:
                jLabel6.setText("MODIFICAR UN CLIENTE");
                jTextField1.setEditable(false);
                jTextField2.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                break;
            case VENTANA_BUSQUEDA:
                jLabel6.setText("BUSCAR UN PRODUCTO");
                jTextField1.setEditable(false);
                jTextField2.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                //BotonCancelar.setVisible(false);
                //BotonCancelar.setText("NUEVO");
                break;
            case VENTANA_ELIMINAR:
                jLabel6.setText("ELIMINAR UN PRODUCTO");
                jTextField1.setEditable(false);
                jTextField2.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                //BotonCancelar.setVisible(false);
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(7, 1));

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel6.setText("Titulo VClientes");
        jPanel2.add(jLabel6);

        getContentPane().add(jPanel2);

        jPanel3.setLayout(new java.awt.GridLayout(1, 2));

        jLabel1.setText("Nombre");
        jPanel3.add(jLabel1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jPanel3.add(jTextField1);

        getContentPane().add(jPanel3);

        jPanel4.setLayout(new java.awt.GridLayout(1, 2));

        jLabel2.setText("sexo");
        jPanel4.add(jLabel2);
        jPanel4.add(jTextField2);

        getContentPane().add(jPanel4);

        jPanel5.setLayout(new java.awt.GridLayout(1, 2));

        jLabel3.setText("Id");
        jPanel5.add(jLabel3);

        jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField3KeyPressed(evt);
            }
        });
        jPanel5.add(jTextField3);

        getContentPane().add(jPanel5);

        jPanel6.setLayout(new java.awt.GridLayout(1, 2));

        jLabel4.setText("Telefono");
        jPanel6.add(jLabel4);
        jPanel6.add(jTextField4);

        getContentPane().add(jPanel6);

        jPanel7.setLayout(new java.awt.GridLayout(1, 2));

        jLabel5.setText("Dirección");
        jPanel7.add(jLabel5);
        jPanel7.add(jTextField5);

        getContentPane().add(jPanel7);

        jPanel8.setLayout(new java.awt.GridLayout(1, 2));

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel8.add(jButton1);

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel8.add(jButton2);

        getContentPane().add(jPanel8);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                Cliente c = new Cliente();
                c.setNombre(jTextField1.getText());
                c.setSexo(jTextField2.getText());
                c.setId(Integer.parseInt(jTextField3.getText()));
                c.setTelefono(Integer.parseInt(jTextField4.getText()));
                c.setDireccion(jTextField5.getText());
                clienteControl.clienregistrar(c);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                break;
            case VENTANA_BUSQUEDA:
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                break;
            case VENTANA_MODIFICAR:
                c = new Cliente();
                c.setNombre(jTextField1.getText());
                c.setSexo(jTextField2.getText());
                c.setId(Integer.parseInt(jTextField3.getText()));
                c.setTelefono(Integer.parseInt(jTextField4.getText()));
                c.setDireccion(jTextField5.getText());
                clienteControl.clienmodificar(c);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                jTextField1.setEditable(false);
                jTextField2.setEditable(false);
                jTextField4.setEditable(false);
                jTextField5.setEditable(false);
                jTextField3.setEditable(true);
                break;
                case VENTANA_ELIMINAR:
                c = new Cliente();
                c.setNombre(jTextField1.getText());
                c.setSexo(jTextField2.getText());
                c.setId(Integer.parseInt(jTextField3.getText()));
                c.setTelefono(Integer.parseInt(jTextField4.getText()));
                c.setDireccion(jTextField5.getText());
                clienteControl.clieneliminar(c);
                jTextField1.setText("");
                jTextField2.setText("");
                jTextField3.setText("");
                jTextField4.setText("");
                jTextField5.setText("");
                break;
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField3KeyPressed
        if(tipoVentana!=TipoVentana.VENTANA_REGISTRO){
            if(evt.getKeyCode()==KeyEvent.VK_ENTER){
                Cliente c=clienteControl.clienbuscarPorCodigo(Integer.parseInt(jTextField3.getText()));
                if(c!=null){
                    jTextField1.setText(c.getNombre());
                    jTextField2.setText(c.getSexo());
                    jTextField3.setText(c.getId()+"");
                    jTextField4.setText(c.getTelefono()+"");
                    jTextField5.setText(c.getDireccion());
                    if(tipoVentana==TipoVentana.VENTANA_MODIFICAR){
                        jTextField1.setEditable(true);
                        jTextField2.setEditable(true);
                        jTextField3.setEditable(false);
                        jTextField4.setEditable(true);
                        jTextField5.setEditable(true);
                    }
                }
            }
        }
    }//GEN-LAST:event_jTextField3KeyPressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables
}
